package com.lscdu.gulimall.ruoyi_springboot.service;

import com.lscdu.gulimall.ruoyi_springboot.bean.login.SysRole;
import com.lscdu.gulimall.ruoyi_springboot.bean.login.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

//用户权限处理
@Component
public class SysPermissionService {

	@Autowired
	private ISysMenuService menuService;

	/**
	 * 获取菜单数据权限
	 *
	 * @param user 用户信息
	 * @return 菜单权限信息
	 */
	public Set<String> getMenuPermission(SysUser user) {
		Set<String> perms = new HashSet<String>();
		//判断该用户是否是管理员 【拥有所有权限】
		if(user.isAdmin()){
			perms.add("*:*:*");
		}else {
			// 用户角色ID查询
			List<SysRole> roles = user.getRoles();
			if(!CollectionUtils.isEmpty(roles)){
				for (SysRole role : roles) {
					Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
					role.setPermissions(rolePerms);
					perms.addAll(rolePerms);
				}
			} else {
				//用户ID查询
				perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
			}
		}
		return perms;
	}
}
